package service

import (
	"fmt"
	"rongyy/src/models"
)

type DoctorService struct {
	d *models.DoctorModel
}

func (ds *DoctorService) FindBySn(sn string) (*models.DoctorModel, error) {
	dm := &models.DoctorModel{}
	doctor, has, err := dm.GetDoctorByIdM(sn)
	if !has {
		return nil, nil
	}
	if err != nil {
		return nil, err
	}
	return doctor, nil
}

func (ds *DoctorService) Query(limit int, offset int, name string) ([]models.DoctorModel, int64, error) {
	dm := &models.DoctorModel{}
	doctor, total, err := dm.Query(limit, offset, name)
	if total > 0 {
		for i := 0; i < len(doctor); i++ {
			doctor[i].NewImage = "https://orm.assetbigdata.com/file/" + doctor[i].NewImage
		}
	}
	if err != nil {
		return nil, 0, err
	}
	return doctor, total, nil
}

func (ds *DoctorService) Update(json *models.DoctorModel) error {
	err := json.Update()
	if err != nil {
		return err
	}
	return nil
}

func (ds *DoctorService) Add(req *models.DoctorModel) error {
	_, has, err := req.GetDoctorByIdM(req.Sn)
	if err != nil {
		return err
	}
	if has {
		return fmt.Errorf("此医生工号已存在")
	}
	err = req.Create()
	if err != nil {
		return err
	}
	return nil
}

func (ds *DoctorService) Delete(req *models.DoctorModel) error {
	_, has, err := req.GetDoctorByIdM(req.Sn)
	if err != nil {
		return err
	}
	if !has {
		return fmt.Errorf("医生信息不存在")
	}
	err = req.Delete()
	if err != nil {
		return err
	}
	return nil
}

func (ds *DoctorService) UpdateSn() error {
	doc := &models.DoctorModel{}
	err := doc.UpdateSn()
	if err != nil {
		return err
	}

	return nil
}
